<?php
session_start();
require_once 'Controlador/VarSite.php';
class LogueoController {	
    private $db;
    private $option2;
    public function __construct(){
        $this->db = BdController::darConexion();
    }
    
    public function iniciarSesion_view(){
        echo 
            '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1. 0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>Iniciar Sesi&oacute;n - FSR</title>
                <link href="Publico/css/estilos.css" rel="stylesheet" type="text/css">
            </head>
            <body class="gradient_log">
                <br><br><br>
                <table height="85%" width="100%" >
                    <tr>
                        <td valign="middle">
                            <table align="center" bgcolor="#336699" cellpadding="1" cellspacing="0">
                            <tr>
                                <td>
                                    <table align="center" bgcolor="#FFFFFF" cellpadding="0" cellspacing="0">
                                        <tr><td><img alt="seguridad" src="Publico/imagenes/user-inicio.png" /></td>
                                            <td>
                                                <table bgcolor="#FFFFFF" cellpadding="5" cellspacing="0">
                                                    <tr align="center">
                                                        <td colspan="2" class="titulo">INICIAR SESI&Oacute;N</td>
                                                    </tr>
                                                    <form action="validar.php" method="post">
                                                    <tr>
                                                        <td>Usuario :</td>
                                                        <td> <input name="user" class="txtBox" type="text"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td>Contrase&ntilde;a :</td>
                                                        <td><input name="clave" class="txtBox" type="password"/></td>
                                                    </tr>
                                                    <tr><td colspan="2" align="center" ><input name="entrar" value="Enviar Datos" class="go" type="submit" /></td></tr>
                                                    </form>
                                                </table>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </body>
        </html>';
    }
        
    public function valida($cadena){
        return ereg_replace("[^A-Za-z0-9?]", "", $cadena);
    }
        
    public function Realizar_Validacion($user,$pass) {
        $user = $this->valida($user); 
        $pass = $this->valida($pass); 
        try{
            $consulta = $this->db->prepare('CALL sp_validarusuario(?,?)');
            if ($consulta->execute(array($user,$pass))){
                while($item = $consulta->fetch()){
                    //if($item['cantidad']==1){					
                    $_SESSION['login_ok'] = true;
                    $_SESSION['usuario'] = $item['Usuario'];
                    $_SESSION['idTipoUsuario'] = $item['CodTipoUsuario'];
                    $operation=$item['CodTipoUsuario'];
                    //}					
                }	   	
            }			
        }catch(PDOException $e){
            echo $e->getMessage();
            echo 'Error';
        }	
        return $operation;
    }
    
    public function ValidaURL($tipo,$url){
        settype($tipo,"integer");
        settype($url,"string");
        $operacion=0;
        try{
            $consulta = $this->db->prepare('CALL sp_validarURL(?,?)');
            if ($consulta->execute(array($tipo,$url))){
                while($item = $consulta->fetch()){
                    $operation=$item['cantidad'];									
                }
            }				
        }catch(PDOException $e){
            echo $e->getMessage();
            echo 'Error';
        }	
	return $operation;			
    }
    
    public function muestraError(){	
        echo '
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>Untitled Document</title>
                <link href="Publico/css/estilos.css" rel="stylesheet" type="text/css">
            </head>
            <body class="gradient_log">
                <table align="center" border="0" cellpadding="2" cellspacing="1" width="730" height="80">
                    <tbody><tr> 
                        <td align="center" valign="middle"> <span class="tplano_20pxbold">ACCESO NO PERMITIDO</span></td>
                    </tr>
                    </tbody>
                </table>
                <br>  
                <table align="center" border="0" cellpadding="2" cellspacing="1" width="730">
                    <tbody><tr valign="top"> 
                        <td align="center" height="343"> 
                            <table border="0" cellpadding="0" cellspacing="0">
                                <tbody><tr valign="top"> 
                                    <td class="titulo" height="25"> 
                                        La 
                                        pagina a la que usted desea acceder no puede ser visualizada
                                        ya que pertenece a una categoria restringida.              
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                        </td>
                        </tr>
                        <tr><td>
                        </td></tr>
                    </tbody>
                </table>
            </body>';
    }
    
    public function muestraErrorGrave(){
        echo '
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>Untitled Document</title>
                <link href="Publico/css/estilos.css" rel="stylesheet" type="text/css">
            </head>
            <body class="gradient_log">
                <table align="center" border="0" cellpadding="2" cellspacing="1" width="730" height="80">
                <tbody>
                    <tr align="center">
                        <td>
                            <img alt="denied" src="Publico/imagenes/denied.png" height="150px" width="150px"/>
                        </td>
                    </tr>
                    <tr>
                        <td align="center" valign="middle">
                            <span class="error">ACCESO NO PERMITIDO</span>
                        </td>
                    </tr>
                </tbody>
                </table>
            </body>';
    }
}
?>